package com.hlk.hlk_blog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hlk.hlk_blog.entity.Blog;
import com.hlk.hlk_blog.entity.Type;
import com.hlk.hlk_blog.entity.User;
import com.hlk.hlk_blog.service.BlogService;
import com.hlk.hlk_blog.service.TagService;
import com.hlk.hlk_blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    //定义一个公共方法，获取分类和标签
    private void setTypeAndTag(Model model) {
        model.addAttribute("types", typeService.getAllType());
        model.addAttribute("tags", tagService.getAllTags());
    }


    @GetMapping("/blogs")
    public String blogs(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum, Model model) {
        List<Type> types = typeService.getAllType();
        model.addAttribute("types", types);
        PageHelper.startPage(pageNum, 5);
        List<Blog> blogs = blogService.getAllBlog();
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/blogs";
    }

    //后台搜索博客列表
    @PostMapping("/blogs/search")
    public String search(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                         Blog blog, Model model) {
        PageHelper.startPage(pageNum, 5);
        List<Blog> blogs = blogService.searchBlog(blog);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        //调用上面的公共方法获取分类和标签
        setTypeAndTag(model);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/blogs";
    }

    /*
     * 跳转到博客新增页面，同时将分类和标签显示出来*/
    @GetMapping("/blogs/input")
    public String input(Model model) {
        setTypeAndTag(model);
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }

    /*
     * 去blog编辑页面*/
    @GetMapping("/blogs/{id}/input") //去编辑博客页面
    public String toEditBlog(@PathVariable Long id, Model model) {
        setTypeAndTag(model);
        Blog blog = blogService.findBlogById(id);
        blog.setType(typeService.getTypeById(blog.getTypeId()));
        blog.init();   //将tags集合转换为tagIds字符串
        model.addAttribute("blog", blog);
        //返回一个blog对象给前端th:object
        return "admin/blogs-input";
    }

    /*
     * 新增编辑*/
    @PostMapping("/blogs")
    public String addBlog(Blog blog, HttpSession session, RedirectAttributes attributes) {
        //设置flag
        if(blog.getFlag()==""||blog.getFlag()==null){
            blog.setFlag("原创");
        }
        //设置user
        blog.setUser((User) session.getAttribute("user"));
        //设置userid
        blog.setUserId(blog.getUser().getId());
        //设置type
        blog.setType(typeService.getTypeById(blog.getType().getId()));
        //设置blog中typeId属性
        blog.setTypeId(blog.getType().getId());
        //给blog中的List<Tag>赋值
        blog.setTags(tagService.getTagByString(blog.getTagIds()));
        if (blog.getId() == null) {
            //新增否则修改
            blogService.saveBlog(blog);
        } else {
            blogService.updateBlog(blog);
        }
        attributes.addFlashAttribute("mssage", "新增成功");
        return "redirect:/admin/blogs";

    }

    /*
     * 删除*/
    @GetMapping("/blogs/{id}/delete")
    public String deleteBlogs(@PathVariable Long id, RedirectAttributes attributes) {
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("mssage", "删除成功");
        return "redirect:/admin/blogs";
    }


}
